﻿using Helper;
using HZY.Framework.DependencyInjection;
using Mapster;
using Model;
using Model.Entity;
using Model.EntityInfo;
using SqlSugar;

namespace DAL.Service
{
    public class RecipeService : BaseService<RecipeEntity>, IScopedSelfDependency
    {
        /// <summary>
        /// 获取配方列表
        /// </summary>
        /// <param name="产品型号"></param>
        /// <param name="产品名称"></param>
        /// <returns></returns>
        public async Task<BaseResult<ResPageData<RecipeInfoModel>>> GetRecipeLidtAsync(string 产品型号, string 产品名称, int page, int pageSize)
        {
            var exp = Expressionable.Create<RecipeEntity>();
            exp.AndIF(!string.IsNullOrEmpty(产品型号), item => item.产品型号.Contains(产品型号));
            exp.AndIF(!string.IsNullOrEmpty(产品名称), item => item.产品名称.Contains(产品名称));
            RefAsync<int> total = 0;
            RefAsync<int> totalPage = 0;
            var list = await DB.SqlSugarClient.Queryable<RecipeEntity>()
                .Where(exp.ToExpression())
                .OrderByPropertyName("UpdatedAt", OrderByType.Desc)
              .ToPageListAsync(page, pageSize, total, totalPage);

            return new BaseResult<ResPageData<RecipeInfoModel>>()
            {
                Code = SystemEnums.ResCode.Success,
                Data = new ResPageData<RecipeInfoModel>()
                {
                    List = list.ToArray().Adapt<List<RecipeInfoModel>>(),
                    Total = total,
                    TotalPage = totalPage
                },
                Msg = ""
            };

        }
        /// <summary>
        /// 新增配方
        /// </summary>
        /// <param name="recipe"></param>
        /// <returns></returns>
        public async Task<BaseResult<string>> CreateRecipeAsync(RecipeEntity recipe)
        {
            BaseResult<string> result = new BaseResult<string>();
            bool isExist = await ExistASync(e => e.产品型号 == recipe.产品型号);
            if (isExist)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "当前产品型号已存在！";
                result.Data = "";
                return result;
            }
            int res = await InsertAsync(recipe);
            if (res == 0)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "新增失败！";
                result.Data = "";
                return result;
            }
            result.Code = SystemEnums.ResCode.Success;
            result.Msg = "新增成功！";
            result.Data = "";
            return result;
        }

        /// <summary>
        /// 更新配方
        /// </summary>
        /// <param name="recipe"></param>
        /// <returns></returns>
        public async Task<BaseResult<string>> UpdateRecipeAsync(RecipeEntity recipe)
        {
            BaseResult<string> result = new BaseResult<string>();
            bool isExist = await ExistASync(e => e.Id == recipe.Id);
            if (!isExist)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "配方不存在，请检查配方id！";
                result.Data = "";
                return result;
            }
            bool res = await UpdateAsync(recipe);
            if (!res)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "更新失败！";
                result.Data = "";
                return result;
            }
            result.Code = SystemEnums.ResCode.Success;
            result.Msg = "更新成功！";
            result.Data = "";
            return result;
        }

        /// <summary>
        /// 删除配方
        /// </summary>
        /// <param name="recipe"></param>
        /// <returns></returns>
        public async Task<BaseResult<string>> DelRecipeAsync(RecipeEntity recipe)
        {
            BaseResult<string> result = new BaseResult<string>();
            bool res = await DeleteAsync(recipe);
            if (!res)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "删除失败";
                result.Data = "";
                return result;
            }
            result.Code = SystemEnums.ResCode.Success;
            result.Msg = "删除成功！";
            result.Data = "";
            return result;
        }

        public async Task<BaseResult<string>> UpdateOrInsertRecipeAsync(List<RecipeEntity> recipes)
        {
            BaseResult<string> result = new BaseResult<string>();
            int res = await DB.SqlSugarClient.Storageable<RecipeEntity>(recipes).WhereColumns(it => it.产品型号).ExecuteCommandAsync();
            if (res == 0)
            {
                result.Code = SystemEnums.ResCode.Fail;
                result.Msg = "导入失败";
                result.Data = "";
                return result;
            }
            result.Code = SystemEnums.ResCode.Success;
            result.Msg = "导入成功！";
            result.Data = "";
            return result;
        }
    }
}
